/**
 * 
 */
package demo.tellurium.test.cqp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import demo.tellurium.framework.BaseTelluriumJUnitTestCase;
import demo.tellurium.module.cqp.CQPLoginModule;
import demo.tellurium.module.cqp.CQPProfileModule;

/**
 * @author roger.xu
 * 
 */
public class ReviewAccessmentJUnitTestCase extends BaseTelluriumJUnitTestCase {
    private static CQPLoginModule cqpLogin;
    private static CQPProfileModule cqpProfile;

    private static List<String> items = new ArrayList<String>();

    @BeforeClass
    public static void initUi() {
        cqpLogin = new CQPLoginModule();
        cqpLogin.defineUi();

        cqpProfile = new CQPProfileModule();
        cqpProfile.defineUi();
    }

    @Before
    public void waitDelay() {
        waitingDelay();
    }

    @Test
    public void testReadFile() {
        console.debug("[testReadFile]");
        InputStream input = ReviewAccessmentJUnitTestCase.class.getResourceAsStream("link_local.txt");

        try {
            List<String> lines = IOUtils.readLines(input);
            items.addAll(lines);
            console.debug(items);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Test
    public void testItems() {
        console.debug("[testItems]");
        console.debug(items);
    }

    @Test
    public void connectTo() {
        String contextUrl = getProperty("cqp.context.url");
        connectUrl(contextUrl);
    }

    @Test
    public void testLogin() {
        console.debug("[testLogin]");
        // cqpLogin.doAdminLogin();
        String username = getProperty("cqp.login.username");
        String password = getProperty("cqp.login.password");
        cqpLogin.doLogin(username, password);
        pauseSec(3);
    }

    @Test
    public void testReview() {
        pauseSec(3);
        run();
    }

    private void run() {
        console.debug(items);
        for (String param : items) {
            String url = getUrl(param);
            console.debug(url);
            connectUrl(url);
            manualBreak();
            cqpProfile.doClickNextButton();
            manualBreak();
        }
    }

    private String getUrl(String param) {
        String contextUrl = getProperty("cqp.context.url");
        String pagePath = getProperty(CQPProfileModule.PATH_KEY);
        String url = String.format("%s/%s?%s", contextUrl, pagePath, param);
        return url;
    }
}
